﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Week3_1_Entity.DTO;
using Week3_1_Entity;
using Week3_1_Service;
using Yitter.IdGenerator;

namespace Week3_1_API.Controllers
{
    /// <summary>
    /// 订单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        OService seRep;
        ILogger<OrderController> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="seRep"></param>
        /// <param name="logger"></param>
        public OrderController(OService seRep, ILogger<OrderController> logger)
        {
            this.seRep = seRep;
            this.logger = logger;
        }
        /// <summary>
        ///用户登录方法
        /// </summary>
        /// <param name="Uname">用户名</param>
        /// <param name="Upwd">用户密码</param>
        /// <returns>返回受影响的实体</returns>
        [HttpGet]
        public IActionResult GetUser(string Uname, string Upwd)
        {
            try
            {
                var list=seRep.GetUser(Uname, Upwd);
                return Ok(new {list.code,list.msg,list.uname,list.upwd,list.uid});
            }
            catch (Exception ex)
            {
                logger.LogError("异常处理：返回用户登录方法出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 雪花ID
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetXue(int count = 10)
        {
            try
            {
               List<long> xue=new List<long>();
                for(int i=0;i<count;i++)
                {
                    var x = YitIdHelper.NextId();
                    xue.Add(x);
                }
                return Ok(xue);
            }
            catch (Exception ex)
            {
                logger.LogError("异常处理：返回雪花ID出错"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 显示订单列表
        /// </summary>
        /// <param name="Uid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrderDTO(int Uid)
        {
            try
            {
                return Ok(seRep.GetOrderDTO(Uid));
            }
            catch (Exception ex)
            {
                logger.LogError("异常处理：返回显示订单商品列表出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示商品列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetGoods()
        {
            try
            {
               return Ok(seRep.GetGoods());
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示商品规格
        /// </summary>
        /// <param name="Gcode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetGoodsSpec(string? Gcode)
        {
            try
            {
                return Ok(seRep.GetGoodsSpec(Gcode));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据SKU改变商品规格
        /// </summary>
        /// <param name="Gcode"></param>
        /// <param name="Svalue"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSKU(string Gcode, string Svalue)
        {
            try
            {
                return Ok(seRep.GetSKU(Gcode, Svalue));
            }
            catch (Exception)
            {

                throw;
            }
        }





















    }
}
